import type { DataQuery } from "@/types/commons/DataQuery";
import type { DataResult } from "@/types/commons/DataResult";


const getDefaultResult = (): DataResult => {
  return {
    totalSize: 0,
    totalPage: 0,
    page: 1,
    pageSize: 10,
  };
};

/**
 * 初始化或更新查询条件
 * @param query - 查询对象
 * @param key - 规则的键
 * @param value - 规则的值
 * @param compara - 比较操作符
 */
const initRule = (
  query: DataQuery | undefined,
  key: string,
  value: string | number,
  compara: string
): void => {
  if (!query) {

    query = { rules: [] };
  }

  if (!query.rules) {

    query.rules = [];
  }


  const existingRuleIndex = query.rules.findIndex((rule) => rule.key === key);

  if (existingRuleIndex !== -1) {

    query.rules[existingRuleIndex] = {
      ...query.rules[existingRuleIndex],
      value,
      compara,
    };
  } else {

    query.rules.push({ key, value, compara });
  }
};

export default { getDefaultResult, initRule };
